#!/bin/bash

# 测试虚拟线程架构的简单脚本

echo "=========================================="
echo "Scene Server 虚拟线程架构测试"
echo "=========================================="

# 检查Java版本
echo "检查Java版本..."
java -version
echo ""

# 检查项目结构
echo "检查项目结构..."
if [ -d "src/main/java/com/gzc/just/play/last/war/sceneserver/battle/manager" ]; then
    echo "✓ EnhancedBattleSessionManager 目录存在"
else
    echo "✗ EnhancedBattleSessionManager 目录不存在"
fi

if [ -f "src/main/java/com/gzc/just/play/last/war/sceneserver/battle/manager/EnhancedBattleSessionManager.java" ]; then
    echo "✓ EnhancedBattleSessionManager.java 文件存在"
else
    echo "✗ EnhancedBattleSessionManager.java 文件不存在"
fi

if [ -f "src/main/java/com/gzc/just/play/last/war/sceneserver/service/BattleMessageRoutingService.java" ]; then
    echo "✓ BattleMessageRoutingService.java 文件存在"
else
    echo "✗ BattleMessageRoutingService.java 文件不存在"
fi

if [ -f "src/main/java/com/gzc/just/play/last/war/sceneserver/service/BattleMessageService.java" ]; then
    echo "✓ BattleMessageService.java 文件存在"
else
    echo "✗ BattleMessageService.java 文件不存在"
fi

if [ -f "src/main/java/com/gzc/just/play/last/war/sceneserver/util/BattleMessageParser.java" ]; then
    echo "✓ BattleMessageParser.java 文件存在"
else
    echo "✗ BattleMessageParser.java 文件不存在"
fi

echo ""

# 检查编译错误
echo "检查编译错误..."
mvn compile -q
if [ $? -eq 0 ]; then
    echo "✓ 项目编译成功"
else
    echo "✗ 项目编译失败，请检查错误"
fi

echo ""

# 运行测试
echo "运行测试..."
mvn test -q
if [ $? -eq 0 ]; then
    echo "✓ 所有测试通过"
else
    echo "✗ 部分测试失败，请检查错误"
fi

echo ""

# 创建简单测试应用
echo "创建简单测试应用..."
cat > test_virtual_threads.java << 'EOF'
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class test_virtual_threads {
    public static void main(String[] args) {
        System.out.println("Java虚拟线程测试");
        
        // 创建虚拟线程执行器
        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
        
        // 创建多个虚拟线程模拟战斗
        for (int i = 1; i <= 5; i++) {
            final int battleId = i;
            executor.submit(() -> {
                String threadName = Thread.currentThread().getName();
                System.out.println("战斗 " + battleId + " 在线程 " + threadName + " 中运行");
                
                // 模拟战斗处理
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                
                System.out.println("战斗 " + battleId + " 处理完成");
            });
        }
        
        // 关闭执行器
        executor.shutdown();
        
        System.out.println("虚拟线程测试完成");
    }
}
EOF

echo "编译并运行测试应用..."
javac test_virtual_threads.java && java test_virtual_threads

# 清理临时文件
rm -f test_virtual_threads.java test_virtual_threads.class

echo ""
echo "=========================================="
echo "测试完成"
echo "=========================================="